home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.03 Mar 91 / Cursor Test Source / Cursor Test.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-18  |  6.5 KB  |  296 lines  |  [TEXT/KAHL]

  1. /**********************************************************
  2. Copyright (c) 1990 Richard Lesh, All Rights Reserved 
  3. **********************************************************/
  4.  
  5. /**********************************************************
  6. Program: Cursor Test
  7.  
  8. Demonstrates the use of animated cursors and exercises the
  9. Cursor Control module.
  10. **********************************************************/
  11.  
  12. #include <MacHeaders>
  13. #include <SANE.h>
  14.  
  15. /**********************************************************
  16. Definitions
  17. **********************************************************/
  18. #ifndef NULL
  19. #define NULL 0L
  20. #endif
  21.  
  22. #define ABOUT_DLOG    128
  23.  
  24. #define APPLE_MENU    128
  25. #define FILE_MENU    129
  26. #define EDIT_MENU    130
  27. #define CURSOR_MENU    131
  28. #define SPEED_MENU    132
  29.  
  30. /**********************************************************
  31. TypeDefs and Enums
  32. **********************************************************/
  33.  
  34. /* Animated cursor control structure 'acur' */
  35. typedef struct {
  36.     short n;
  37.     short index;
  38.     union {
  39.         Handle cursorHdl;
  40.         short resID;
  41.     }frame[1];
  42. }acurRec,*acurPtr,**acurHandle;
  43.  
  44. /* Menu commands */
  45. enum {mAbout=1};
  46. enum {mStart=1,mStop,mQuit=4};
  47. enum {mUndo=1,mCut=3,mCopy,mPaste,mClear};
  48.  
  49. /**********************************************************
  50. Private Globals
  51. **********************************************************/
  52. static short gCursorNum;
  53. static short gSpeedNum;
  54.  
  55. /**********************************************************
  56. Prototypes
  57. **********************************************************/
  58. void main(void);
  59. void InitMgrs(void);
  60. void FillMenus(void);
  61. void EventLoop(void);
  62. void DoMenu(long command);
  63. void DoOpen(void);
  64. void DoClose(void);
  65.  
  66. void InitCursorCtrl(acurHandle h);
  67. void QuitCursorCtrl(void);
  68. void SpinCursor(short cycles);
  69. void StopCursor(void);
  70. void SetCursorSpeed(short newSpeed);
  71.  
  72. void Wait(short);
  73.  
  74. /**********************************************************
  75. Routine: main()
  76.  
  77. Entry point for the application.
  78. **********************************************************/
  79.  
  80. void main()
  81. {
  82.     short i;
  83.  
  84.     MaxApplZone();
  85.     for (i=0;i<10;i++)
  86.         MoreMasters();
  87.     InitMgrs();
  88.     FillMenus();
  89.     DoOpen();
  90.     EventLoop();
  91.     DoClose();
  92. }
  93.  
  94. /**********************************************************
  95. Routine: InitMgrs()
  96.  
  97. This routine initializes all the appropriate managers.
  98. **********************************************************/
  99.  
  100. void InitMgrs()
  101. {
  102.     InitGraf(&thePort);
  103.     InitFonts();
  104.     InitWindows();
  105.     InitMenus();
  106.     TEInit();
  107.     InitDialogs(NULL);
  108.     InitCursor();
  109.     FlushEvents(everyEvent,0);
  110. }
  111.  
  112. /**********************************************************
  113. Routine: FillMenus()
  114.  
  115. Reads in the menu bar and menu resources, then builds the
  116. dynamic menus.
  117. **********************************************************/
  118.  
  119. void FillMenus()
  120. {    
  121.     short i;
  122.     Handle menuBarHdl;
  123.     MenuHandle menuHdl;
  124.     
  125.     menuBarHdl=GetNewMBar(128);     /*Load the Menu Bar */
  126.     SetMenuBar(menuBarHdl);         /*Load the MENUs */
  127.     menuHdl=GetMHandle(APPLE_MENU); /*Build the Apple menu*/
  128.     AddResMenu(menuHdl,'DRVR');
  129.     menuHdl=GetMHandle(CURSOR_MENU);/*Build the Cursor menu*/
  130.     AddResMenu(menuHdl,'acur');
  131.     DrawMenuBar();
  132. }
  133.  
  134. /**********************************************************
  135. Routine: EventLoop()
  136.  
  137. Main event loop processing.
  138. **********************************************************/
  139.  
  140. Boolean gDone=FALSE;
  141. void EventLoop()
  142. {
  143.     EventRecord theEvent;
  144.     WindowPtr theWindow;
  145.     short partCode;
  146.     
  147.     while (!gDone){
  148.         SystemTask();
  149.         GetNextEvent(everyEvent,&theEvent);
  150.         switch(theEvent.what){
  151.             case nullEvent:
  152.                 break;
  153.             case keyDown:
  154.             case autoKey:
  155.                 if (theEvent.modifiers & cmdKey){
  156.                     DoMenu(MenuKey((short)(theEvent.message & charCodeMask)));
  157.                     HiliteMenu(0);
  158.                 }
  159.                 break;
  160.             case updateEvt:
  161.                 break;
  162.             case activateEvt:
  163.                 break;
  164.             case mouseDown:
  165.                 switch (partCode=FindWindow(theEvent.where,&theWindow)){
  166.                     case inDesk: break;
  167.                     case inMenuBar:
  168.                         DoMenu(MenuSelect(theEvent.where));break;
  169.                     case inSysWindow:
  170.                         SystemClick(&theEvent,theWindow);break;
  171.                     default:
  172.                         break;
  173.                 }
  174.                 break;
  175.             case app4Evt:
  176.                 if (!(theEvent.message&suspendResumeMessage)) StopCursor();
  177.                 break;
  178.         }
  179.     }
  180. }
  181.  
  182. /**********************************************************
  183. Routine: DoMenu(long command)
  184.  
  185. Handles menu selections.
  186. **********************************************************/
  187.  
  188. void DoMenu(long command)
  189. {
  190.     short menu_id=HiWord(command);
  191.     short item=LoWord(command);
  192.     Str255 item_name;
  193.     MenuHandle mHdl;
  194.     DialogPtr dPtr;
  195.     
  196.     switch(menu_id){
  197.         case APPLE_MENU:
  198.             if (item==1){
  199.                     dPtr=GetNewDialog(ABOUT_DLOG, NULL, -1);
  200.                     while (ModalDialog(NULL, &item),item!=1);
  201.                     DisposDialog(dPtr);
  202.             }else if (item>2){
  203.                 GetItem(GetMHandle(menu_id),item,item_name);
  204.                 OpenDeskAcc(item_name);
  205.             }
  206.             break;
  207.         case EDIT_MENU:
  208.             SystemEdit(item-1);break;
  209.         case FILE_MENU:
  210.             switch(item){
  211.                 case mStart:
  212.                     SpinCursor(5);
  213.                     Wait(6*60);
  214.                     break;
  215.                 case mStop:
  216.                     StopCursor();
  217.                     InitCursor();
  218.                     break;
  219.                 case mQuit:
  220.                     gDone=TRUE;
  221.                     break;
  222.             }
  223.             break;
  224.         case CURSOR_MENU:
  225.             mHdl=GetMHandle(menu_id);
  226.             GetItem(mHdl,item,item_name);
  227.             StopCursor();
  228.             InitCursorCtrl((acurHandle)GetNamedResource('acur',
  229.                 item_name));
  230.             CheckItem(mHdl, gCursorNum, FALSE);
  231.             CheckItem(mHdl, item, TRUE);
  232.             gCursorNum=item;
  233.             break;
  234.         case SPEED_MENU:
  235.             mHdl=GetMHandle(menu_id);
  236.             GetItem(mHdl,item,item_name);
  237.             SetCursorSpeed((short)str2num((char *)item_name));
  238.             CheckItem(mHdl, gSpeedNum, FALSE);
  239.             CheckItem(mHdl, item, TRUE);
  240.             gSpeedNum=item;
  241.             break;            
  242.     }
  243.     HiliteMenu(0);
  244. }
  245.  
  246. /**********************************************************
  247. Routine: DoOpen()
  248.  
  249. Performs tasks that need to be done at startup.
  250. **********************************************************/
  251.  
  252. void DoOpen()
  253. {
  254.     MenuHandle mHdl;
  255.     Str255 s;
  256.     
  257.     gCursorNum=1;    /* Select cursor #1 and update menu    */
  258.     mHdl=GetMenu(CURSOR_MENU);
  259.     CheckItem(mHdl, gCursorNum, TRUE);
  260.     GetItem(mHdl,gCursorNum,s);
  261.     InitCursorCtrl((acurHandle)GetNamedResource('acur',s));
  262.  
  263.     gSpeedNum=5;    /* Select speed #5 and update menu    */
  264.     mHdl=GetMenu(SPEED_MENU);
  265.     CheckItem(mHdl, gSpeedNum, TRUE);
  266.     GetItem(mHdl,gSpeedNum,s);
  267.     SetCursorSpeed((short)str2num((char *)s));
  268. }
  269.  
  270. /**********************************************************
  271. Routine: DoClose()
  272.  
  273. Cleans up before shutting down the application.
  274. **********************************************************/
  275.  
  276. void DoClose()
  277. {
  278.     QuitCursorCtrl();
  279. }
  280.  
  281. /**********************************************************
  282. Routine:    Wait(short t)
  283.  
  284. Pauses execution for t sixtieths of a second.  Does not
  285. allow background tasks to execute.
  286. **********************************************************/
  287.  
  288. void Wait(short t)
  289. {
  290.     long s;
  291.     
  292.     s=TickCount()+t;
  293.     while (TickCount()<s);
  294. }
  295.  
  296.